﻿
//钩子函数
$(function () {
    getFinishedProductData();
});

var obj = {
    finishedProductId: '',
    finishedProductNo: '',
    finishedProductName: '',
    finishedProductSpecification: '',
    finishedProductBatch: '',
    finishedProductUnit: '',
    finishedProductState: '',
    inventoryLimit: '',
    inventoryMinimum: '',
    alarmDays: '',
    finishedProductImg: '',
    finishedProductRemark: '',
    createdBy: '',
    createdDate: '',
    updatedBy: '',
    updatedDate: '',
    deletedBy: '',
    deletedDate: '',
    isDeleted: ''
}

//返填
function getFinishedProductData() {
    layui.use(function () {
        var form = layui.form;
        var finishedProductId = localStorage.getItem("finishedProductId");
        $.ajax({
            url: '/Warehouse/FinishedProductBackfill',
            type: 'get',
            data: {
                finishedProductId: finishedProductId,
                token: localStorage.getItem("token")
            },
            success: function (res) {
                var res = JSON.parse(res)
                console.log(res)
                $.each(res, function () {
                    $("#finishedProductNo").val(this.finishedProductNo)
                    $("#finishedProductName").val(this.finishedProductName)
                    $("#finishedProductSpecification").val(this.finishedProductSpecification)
                    $("#finishedProductBatch").val(this.finishedProductBatch)
                    $("#finishedProductUnit").val(this.finishedProductUnit)
                    $("#inventoryLimit").val(this.inventoryLimit)
                    //状态
                    if (this.finishedProductState == true) {
                        $('#normal').prop('checked', true)
                    } else {
                        $('#abnormal').prop('checked', true)
                    }
                    $("#inventoryMinimum").val(this.inventoryMinimum)
                    $("#alarmDays").val(this.alarmDays)
                    $("#finishedProductImg").val(this.finishedProductImg)
                    $("#finishedProductRemark").val(this.finishedProductRemark)
                    $("#createdBy").val(this.createdBy)
                    obj.finishedProductId = this.finishedProductId,
                        obj.finishedProductNo = this.finishedProductNo,
                        obj.finishedProductName = this.finishedProductName,
                        obj.finishedProductSpecification = this.finishedProductSpecification,
                        obj.finishedProductBatch = this.finishedProductBatch,
                        obj.finishedProductUnit = this.finishedProductUnit,
                        obj.finishedProductState = this.finishedProductState,
                        obj.inventoryLimit = this.inventoryLimit,
                        obj.inventoryMinimum = this.inventoryMinimum,
                        obj.alarmDays = this.alarmDays,
                        obj.finishedProductImg = this.finishedProductImg,
                        obj.finishedProductRemark = this.finishedProductRemark,
                        obj.createdBy = this.createdBy,
                        obj.createdDate = this.createdDate,
                        obj.updatedBy = this.updatedBy,
                        obj.updatedDate = this.updatedDate,
                        obj.deletedBy = this.deletedBy,
                        obj.deletedDate = this.deletedDate,
                        obj.isDeleted = this.isDeleted,
                        form.render(); // 重新渲染
                })
            },
        })
    })
}

//修改
layui.use(['layer', 'form', 'util'], function () {
    var layer = layui.layer;
    var $ = layui.jquery;
    var form = layui.form;
    var util = layui.util;

    form.on('submit(formDemo)', function (data) {
        var dto = data.field; // 获取表单字段值
        data.field.finishedProductId = obj.finishedProductId
        data.field.createdBy = obj.createdBy
        data.field.updatedBy = obj.updatedBy
        console.log(data);
        $.ajax({
            url: '/Warehouse/PostFinishedProductUpd',
            type: 'post',
            data: {
                dto: dto,
                token: localStorage.getItem("token")
            },
            success: function (res) {
                var res = JSON.parse(res)
                console.log(dto);
                console.log(res);
                if (res.code == 0) {
                    layer.alert("修改成功");
                    setTimeout(function () {
                        location.href = '/Warehouse/FinishedProductShow'
                    }, 2000);
                } else {
                    layer.alert("修改失败");
                }
            }
        });

        return false; // 阻止默认 form 跳转
    });
    // 对弹层中的表单进行初始化渲染
    form.render();
});